﻿Imports Controle
Imports Persistencia
Public Class frmAgendaVisita

#Region "Variáveis"
    Private prvIDCliente As Int32
    Private prvIDRegiao As Int32
    Private prvIDUsuario As Int32
    Private prvIDClienteSel As Int32
    Private prvIDContatoSel As Int32
    Private prvDataVisitaSel As Date

    Dim objCliente As New ctrCliente
    Dim objConversao As New perConversao
    Dim objRegiao As New ctrRegiao
    Dim objStatus As New ctrStatus
    Dim objVisitas As New ctrVisita
    Dim objUsuarios As New ctrUsuario

#End Region

#Region "Enumerações"

    Private Enum eColAgenda

        eDataVisita = 0
        eIdCliente = 1
        eNomeCliente = 2
        eIdContato = 3
        eNomeContato = 4
        eIdRegiao = 5
        eRegiao = 6
        eIDStatus = 7
        eRealizada = 8

    End Enum
#End Region

#Region "Métodos privados"

    Private Sub FormataDtGrid()
        Dim Colunas As New DataGridViewTextBoxColumn()
        Try
            With dtGridAgenda
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .AutoGenerateColumns = False
                .RowHeadersVisible = False

                .Columns.Clear()
                'define cada coluna do grid

                Dim Col_Data As New DataGridViewTextBoxColumn()
                With Col_Data
                    .DataPropertyName = "data"
                    .HeaderText = "Data"
                    .Width = 75
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                    .Visible = True
                End With
                .Columns.Add(Col_Data)

                Dim Col_IDCliente As New DataGridViewTextBoxColumn()
                With Col_IDCliente
                    .DataPropertyName = "idCliente"
                    .HeaderText = "IDCliente"
                    .Width = 15
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = False
                End With
                .Columns.Add(Col_IDCliente)

                Dim Col_Cliente As New DataGridViewTextBoxColumn()
                With Col_Cliente
                    .DataPropertyName = "nomeCliente"
                    .HeaderText = "Cliente"
                    .Width = 170
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = True
                End With
                .Columns.Add(Col_Cliente)

                Dim Col_IDContato As New DataGridViewTextBoxColumn()
                With Col_IDContato
                    .DataPropertyName = "idContato"
                    .HeaderText = "IDContato"
                    .Width = 15
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = False
                End With
                .Columns.Add(Col_IDContato)

                Dim Col_Contato As New DataGridViewTextBoxColumn()
                With Col_Contato
                    .DataPropertyName = "nomeContato"
                    .HeaderText = "Contato"
                    .Width = 118
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = True
                End With
                .Columns.Add(Col_Contato)

                Dim Col_IDRegiao As New DataGridViewTextBoxColumn()
                With Col_IDRegiao
                    .DataPropertyName = "idRegiao"
                    .HeaderText = "IDRegiao"
                    .Width = 15
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = False
                End With
                .Columns.Add(Col_IDRegiao)

                Dim Col_Regiao As New DataGridViewTextBoxColumn()
                With Col_Regiao
                    .DataPropertyName = "Regiao"
                    .HeaderText = "Regiao"
                    .Width = 100
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .Visible = True
                End With
                .Columns.Add(Col_Regiao)

                Dim Col_Status As New DataGridViewTextBoxColumn()
                With Col_Status
                    .DataPropertyName = "Status"
                    .HeaderText = "Status"
                    .Width = 80
                    .Frozen = False
                    .ReadOnly = True
                    .FillWeight = 100
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
                    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                    .Visible = True
                End With
                .Columns.Add(Col_Status)

                Dim Col_Realizada As New DataGridViewCheckBoxColumn()
                With Col_Realizada
                    .DataPropertyName = "Realizada"
                    .HeaderText = "Realizada"
                    .Width = 80
                    .Frozen = False
                    .ReadOnly = True
                    .ValueType = System.Type.GetType("Boolean")
                    .DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                    .Visible = True
                End With
                .Columns.Add(Col_Realizada)

                .Refresh()

            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub

    Private Sub PreencherDtGrid()
        Dim iIDStatus As Int32
        Dim dsVisitas As New DataSet
        Dim dtInicio As Date
        Dim dtFim As Date
        Dim dtData As Date
        Dim IncluirRealizadas As Boolean

        Try
            If (chkDatas.Checked = False) Then
                dtInicio = CDate(dtDataInicio.Text)
                dtFim = CDate(dtDataFim.Text)
            Else
                dtInicio = Date.MinValue
                dtFim = Date.MinValue
            End If
            IncluirRealizadas = chkIncluirRealizadas.Checked

            dsVisitas = Nothing
            dsVisitas = objVisitas.dsVisitasPorPeriodo(dtInicio, dtFim, prvIDCliente, prvIDRegiao, 0, prvIDUsuario, IncluirRealizadas)
            dtGridAgenda.Rows.Clear()

            If (Not dsVisitas Is Nothing AndAlso dsVisitas.Tables("DC_CadVisitas").Rows.Count > 0) Then
                dtGridAgenda.RowCount = dsVisitas.Tables("DC_CadVisitas").Rows.Count
                With dsVisitas.Tables("DC_CadVisitas")
                    For cont As Integer = 0 To .Rows.Count - 1
                        dtGridAgenda.Item(eColAgenda.eIdCliente, cont).Value = perConversao.nuloParaZero(.Rows(cont).Item("idcliente"))
                        dtGridAgenda.Item(eColAgenda.eNomeCliente, cont).Value = perConversao.nuloParaVazio(.Rows(cont).Item("razaoSocial"))
                        dtGridAgenda.Item(eColAgenda.eIdContato, cont).Value = perConversao.nuloParaZero(.Rows(cont).Item("idcontato"))
                        dtGridAgenda.Item(eColAgenda.eNomeContato, cont).Value = perConversao.nuloParaVazio(.Rows(cont).Item("nomeContato"))
                        dtGridAgenda.Item(eColAgenda.eIdRegiao, cont).Value = perConversao.nuloParaZero(.Rows(cont).Item("IDRegiao"))
                        dtGridAgenda.Item(eColAgenda.eRegiao, cont).Value = perConversao.nuloParaVazio(.Rows(cont).Item("Regiao"))
                        dtData = CDate(perConversao.nuloParaData(.Rows(cont).Item("DataVisita")))
                        dtGridAgenda.Item(eColAgenda.eDataVisita, cont).Value = dtData.ToShortDateString
                        iIDStatus = perConversao.nuloParaZero(.Rows(cont).Item("idStatus"))
                        If (iIDStatus > 0) Then
                            dtGridAgenda.Item(eColAgenda.eIDStatus, cont).Value = objStatus.retornarCampoStatus("Descricao", iIDStatus).Item("Descricao")
                        End If
                        dtGridAgenda.Item(8, cont).Value = CBool(.Rows(cont).Item("Realizada"))
                    Next
                End With

                dtGridAgenda.SelectionMode = DataGridViewSelectionMode.FullRowSelect
                dtGridAgenda.Rows(0).Selected = False
                lblNVisitas.Text = dtGridAgenda.RowCount
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try
    End Sub

#End Region

  
    Private Sub frmAgendaVisita_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated

        Call PreencherDtGrid()

    End Sub

    Private Sub frmAgendaVisita_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Call FormataDtGrid()

    End Sub

    Private Sub cmdFiltrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFiltrar.Click

        Call PreencherDtGrid()

    End Sub

    Private Sub cmdProcCliente_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcCliente.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa
        sql = Me.objCliente.SqlConsulta(False)

        prvIDCliente = 0

        Limpar(Me)

        With frmDialogo
            .Campos = "iIDCliente,sRazaoSocial"
            .Descricoes = "Código,Cliente"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa Cliente"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    Me.Codigo = .Id
                    Me.prvIDCliente = .Id
                    Me.txtIDCliente.Text = .Id
                    txtNomeCliente.Text = objCliente.selecionarCliente(prvIDCliente).Item("RazaoSocial")
                    Me.modoAtual = eModo.eAguardar
                    chkClientes.Checked = False
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Me.prvIDCliente = 0
                MyBase.Limpar(Me)
                Exit Sub
            End If

        End With
    End Sub

    Private Sub cmdProcRegiao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcRegiao.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa

        sql = Me.objRegiao.sqlConsulta

        With frmDialogo
            .Campos = "iIDRegiao,sRegiao"
            .Descricoes = "Código,Região"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa Região"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    Me.prvIDRegiao = .Id
                    Me.txtRegiao.Text = objRegiao.retornarCampoRegiao("Regiao", prvIDRegiao).Item("Regiao").ToString
                    chkRegiao.Checked = False
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
            End If
        End With
    End Sub

    Private Sub chkClientes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkClientes.CheckedChanged
        If (chkClientes.Checked = True) Then
            prvIDCliente = 0
            txtIDCliente.Text = ""
            txtNomeCliente.Text = ""
        End If
    End Sub

    Private Sub chkRegiao_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRegiao.CheckedChanged
        If (chkRegiao.Checked = True) Then
            prvIDRegiao = 0
            txtIDRegiao.Text = ""
            txtRegiao.Text = ""
        End If
    End Sub

    Private Sub chkDatas_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDatas.CheckedChanged

        If (chkDatas.Checked = True) Then
            dtDataInicio.Enabled = False
            dtDataFim.Enabled = False
        Else
            dtDataInicio.Enabled = True
            dtDataFim.Enabled = True
        End If

    End Sub

    Private Sub cmdAdicionar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdicionar.Click

        frmCadVisitas.ShowDialog()

    End Sub

    Private Sub dtGridAgenda_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtGridAgenda.CellClick
        If (e.RowIndex >= 0) Then
            With dtGridAgenda
                prvIDClienteSel = CInt(.Rows(e.RowIndex).Cells(eColAgenda.eIdCliente).Value.ToString())
                prvIDContatoSel = CInt(.Rows(e.RowIndex).Cells(eColAgenda.eIdContato).Value.ToString())
                prvDataVisitaSel = CDate(.Rows(e.RowIndex).Cells(eColAgenda.eDataVisita).Value.ToString())
            End With
        End If
    End Sub

    Private Sub cmdEditarContato_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEditarContato.Click
        If (prvIDClienteSel > 0 And prvIDContatoSel > 0 And prvDataVisitaSel <> Date.MinValue) Then
            With frmCadVisitas
                .Cliente = prvIDClienteSel
                .Contato = prvIDContatoSel
                .DataVisita = prvDataVisitaSel

                .ShowDialog()

                Call PreencherDtGrid()
            End With
        End If
    End Sub

    Private Sub cmdExcluiContato_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExcluiContato.Click

        If (dtGridAgenda.SelectedRows.Count > 0) Then
            If (MsgBox("Confirma exclusão da visita selecionada?", MsgBoxStyle.YesNo, "Confirmação") = MsgBoxResult.Yes) Then
                If (objVisitas.ExcluirVisitaAgendada(prvIDClienteSel, prvIDContatoSel, prvDataVisitaSel)) Then
                    MsgBox("Visita excluída com sucesso.", MsgBoxStyle.OkOnly)
                End If
                Call PreencherDtGrid()
            End If
        End If
    End Sub

    Private Sub cmdProcurarUsuario_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcurarUsuario.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa

        sql = Me.objUsuarios.sqlConsulta

        With frmDialogo
            .Campos = "iIDUsuario,sNome"
            .Descricoes = "Código,Nome"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa de usuários"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    prvIDUsuario = .Id
                    Me.txtNomeUsuario.Text = objUsuarios.retornarCampoUsuario("Nome", prvIDUsuario).Item("Nome")
                    chkUsuarios.Checked = False
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Exit Sub
            End If

        End With
    End Sub

    Private Sub chkUsuarios_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkUsuarios.CheckedChanged

        If (chkUsuarios.Checked = True) Then
            prvIDUsuario = 0
        End If

    End Sub

    Private Sub cmdHistVisita_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHistVisita.Click
        Dim frmHistorico As New frmHistVisita

        With frmHistorico
            .IDCliente = prvIDClienteSel
            .IDContato = prvIDContatoSel
            .DataVisita = prvDataVisitaSel.Date
            .CarregarDados()
            .ShowDialog()

        End With

    End Sub

End Class
